<?php

class MemTypeDiscountController extends BaseController
{

	/**
	 * 等级优惠管理
	 */
	public function actionAdmin()
	{

		$model = new CmpPromotion;
		$model->unsetAttributes();//清空属性
		$model->zm_id = Company::model()->getCmpUserId();
		$model->zcr_type = 7;//类型为等级优惠
		$model->zcr_status = array(1,2);//状态不为删除
		$this->render('admin',array(
				'model'=>$model,
				));
	
	}
	
	/**
	 * 创建等级优惠
	 */
	public function actionCreate(){
		$model = new CmpPromotion();
		
		if(isset($_POST['CmpPromotion'])){
			$model->attributes = $_POST['CmpPromotion'];
			$model->zm_id = Company::model()->getCmpUserId();
			$model->zcr_type = 7;//类型为等级优惠
			$model->zcr_start_time = strtotime($model->zcr_start_time);
			$model->zcr_end_time = strtotime($model->zcr_end_time);
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
			print_r($model->zm_id);exit;
		}
		//准备检索产品
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->zm_id = Company::model()->getCmpUserId();
		if(isset($_GET['Product']))
			$product->attributes = $_GET['Product'];
		
		$this->render('create',array(
				'model'=>$model,
				'product'=>$product,
		));
	}
	
	/**
	 * 修改等级优惠
	 */
	public function actionUpdate($id){
		$model = CmpPromotion::model()->findByPk($id);
		$model->zcr_start_time = date('Y-m-d',$model->zcr_start_time);
		$model->zcr_end_time = date('Y-m-d',$model->zcr_end_time);
		
		if(isset($_POST['CmpPromotion'])){
			$model->attributes = $_POST['CmpPromotion'];
			$model->zcr_start_time = strtotime($model->zcr_start_time);
			$model->zcr_end_time = strtotime($model->zcr_end_time);
			if($model->save()){
				//成功跳转
				$this->redirect(array('admin'));
			}
		}
		
		//准备检索产品
		$product = New Product();
		$product->unsetAttributes();//清空属性
		$product->zm_id = Company::model()->getCmpUserId();
		if(isset($_GET['Product']))
			$product->attributes = $_GET['Product'];
		
		$this->render('create',array(
				'model'=>$model,
				'product'=>$product,
		));
	}

	/**
	 * 取消，启用等级优惠
	 */
	public function actionChange($id){
		$model = CmpPromotion::model()->findByPk($id);
		if($model->zcr_status == 1){
			$model->zcr_status = 2;//状态  是否关闭 1否，2是
		}elseif($model->zcr_status == 2||$model->zcr_end_time>time()){
			$model->zcr_status = 1;//状态  是否关闭 1否，2是
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
	
		if($model->save()){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
	
	/**
	 *单条，批量删除
	 */
	public function actionDelete(){
		//操作ID号格式处理
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}elseif(isset($_POST['ids'])){
			$id =$_POST['ids'];
		}else{
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
		}
		$id = is_array($id)?join(',',$id):(int)$id;
		//处理条件
		$criteria=new CDbCriteria;
		$criteria->addCondition("zcr_id in ($id)");

		//执行操作
		if(CmpPromotion::model()->deleteAll($criteria)){
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
	}
}